package cn.xi.service.checkItem;

import cn.xi.checkItem.SetmealService;
import cn.xi.constant.MessageConstant;
import cn.xi.dao.SetmealDao;
import cn.xi.entity.PageResult;
import cn.xi.entity.QueryPageBean;
import cn.xi.entity.Result;
import cn.xi.pojo.CheckGroup;
import cn.xi.pojo.CheckItem;
import cn.xi.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    //    模板生成html
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${out_put_path}")
    private String out_put_path;

    @Override
    public PageResult pages(QueryPageBean queryPageBean) {
        Page<Setmeal> page = null;
        if (queryPageBean.getQueryString() == null || queryPageBean.getQueryString().length() == 0) {
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            page = setmealDao.pages();
        } else {
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            page = setmealDao.pagesString(queryPageBean.getQueryString());
        }
        List<Setmeal> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public Result findall() {
        List<CheckGroup> findall = setmealDao.findall();
        return new Result(true, null, findall);
    }

    @Override
    public Map selectsetmeal(Integer id) {
        Setmeal selectsetmeal = setmealDao.selectsetmeal(id);
        Integer[] integers = setmealDao.selectSetmealAndGroupDao(id);
        Map map = new HashMap();
        map.put("setmeal", selectsetmeal);
        map.put("groupid", integers);
        return map;
    }

    @Override
    public void delete(Integer id) {
        setmealDao.deleteSetmeal(id);
        setmealDao.delete(id);
        sethtml();
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal byId = setmealDao.findById(id);
        return byId;
    }

    @Override
    public List<Map> selectandorder() {
        return setmealDao.selectandorder();

    }


    @Override
    public Result save(Setmeal setmeal, Integer[] groupid) {
        String a = null;
        String b = null;
        String s = null;
        try {
            Integer id = setmeal.getId();

            if (id != null && id != 0) {
                b = "编辑套餐失败";
                s = setmealDao.selecteImg(id);
                setmealDao.update(setmeal);
                a = "编辑套餐成功";
            } else {
                b = MessageConstant.ADD_SETMEAL_FAIL;
                setmealDao.save(setmeal);
                id = setmealDao.getid();
                a = MessageConstant.ADD_SETMEAL_SUCCESS;
            }
            setmealDao.deleteSetmeal(id);
            for (Integer integer : groupid) {
                setmealDao.savesetmeaandgroup(id, integer);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, b);
        }
        sethtml();
        return new Result(true, a, s);
    }

    //查询数据调用方法生成html
    public void sethtml() {
        List<Setmeal> list = setmealDao.findBy();
        //生成套餐静态页面
        taocanjingtai(list);
        //生成套餐详情页面
        taocanxiangqing(list);
    }

    //生成套餐静态页面
    public void taocanjingtai(List<Setmeal> list) {
        Map<String, List<Setmeal>> map = new HashMap<>();
        map.put("setmealList", list);
        gethtml("mobile_setmeal.ftl", "setmeal2.html", map);
    }

    //生成套餐详情页面
    public void taocanxiangqing(List<Setmeal> list) {
        Map map = new HashMap();
        for (Setmeal setmeal : list) {
            map.put("setmeal", setmeal);
            gethtml("mobile_setmeal_detail.ftl", "setmeal_detail_" + setmeal.getId() + ".html", map);
        }
    }

    //模板名字,     生成出来的html名字, 需要的数据
    public void gethtml(String timpolet, String htmlname, Map map) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer fileWriter = null;
        try {
            Template template = configuration.getTemplate(timpolet);
            fileWriter = new FileWriter(out_put_path + htmlname);
            template.process(map, fileWriter);
            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
